package net.hekka.canyoutalk.share.database;

import net.hekka.canyoutalk.data.constant.ConnectionConstant;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * SQL문의 실행을 관리하는 클래스.
 * @author Jeffrey Ha
 */
public class SqlExecutor {

    private Connection dbConnection = null;
    private PreparedStatement preparedStatement = null;
    private String databaseType = null;

    /**
     * 기본 생성자
     */
    public SqlExecutor() {
    }

    protected void setDbConnection(Connection _dbConnection) {
        dbConnection = _dbConnection;
    }

    protected PreparedStatement getPreparedStatement() {
        return preparedStatement;
    }

    protected void setPreparedStatement(String sql) throws SQLException {
        preparedStatement = dbConnection.prepareStatement(sql);
    }

    protected String getDatabaseType() {
        return databaseType;
    }

    protected void setDatabaseType() {
        databaseType = ConnectionConstant.databaseType;
    }

    protected void setIntPreparedStatement(int sequence, int value) throws SQLException {
        preparedStatement.setInt(sequence, value);
    }
    
    protected void setLongPreparedStatement(int sequence, long value) throws SQLException {
        preparedStatement.setLong(sequence, value);
    }

    protected void setStringPreparedStatement(int sequence, String value) throws SQLException {
        if (value != null) {
            preparedStatement.setString(sequence, value);
        } else {
            preparedStatement.setNull(sequence, java.sql.Types.VARCHAR);
        }
    }

    protected void setCharPreparedStatement(int sequence, String value) throws SQLException {
        if (value != null) {
            preparedStatement.setString(sequence, value);
        } else {
            preparedStatement.setNull(sequence, java.sql.Types.CHAR);
        }
    }
    
    protected void setDatePreparedStatement(int sequence, java.util.Date value) throws SQLException {
        if (value != null) {
            preparedStatement.setTimestamp(sequence, new  java.sql.Timestamp(value.getTime()));
        } else {
            preparedStatement.setNull(sequence, java.sql.Types.DATE);
        }
    }

    protected void setTimestampPreparedStatement(int sequence, java.sql.Timestamp value) throws SQLException {
        if (value != null) {
            preparedStatement.setTimestamp(sequence, value);
        } else {
            preparedStatement.setNull(sequence, java.sql.Types.TIMESTAMP);
        }
    }

    protected void setBooleanPreparedStatement(int sequence, boolean value) throws SQLException {
        preparedStatement.setBoolean(sequence, value);
    }


}
